import os
import sys
import rtconfig

cwd = os.path.normpath(os.getcwd())

RTT_ROOT = os.path.normpath(r'E:\Workspace\software\rt-thread')
#BSP_ROOT = os.path.normpath(os.getcwd())
BSP_ROOT = os.path.normpath(os.path.join(RTT_ROOT, 'bsp/simulator'))
APP_ROOT = os.path.normpath(r'E:\Workspace\software\geomative\applications') 
EXT_ROOT = os.path.normpath(r'E:\Workspace\software\geomative\components')

sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
#sys.path.insert(0, os.path.join(EXT_ROOT, 'rtgui', 'utils'))


from building import *


env = Environment(TARGET_ARCH='x86')


Export('RTT_ROOT')
Export('APP_ROOT')
Export('BSP_ROOT')
Export('EXT_ROOT')
Export('rtconfig')

BUILD_DIR = 'build/'

if rtconfig.PLATFORM == 'cl':
    TARGET = 'rtthread-win32.' + rtconfig.TARGET_EXT

    libs = Split('''
    winmm
    gdi32
    winspool
    comdlg32
    advapi32
    shell32
    ole32
    oleaut32
    uuid
    odbc32
    odbccp32
    ''')
    definitions = Split('''
    WIN32
    _DEBUG
    _CONSOLE
    MSVC
    _TIME_T_DEFINED
    ''')
    env.Append(CCFLAGS=rtconfig.CFLAGS)
    env.Append(LINKFLAGS=rtconfig.LFLAGS)
    env['LIBS']=libs
    env['CPPDEFINES']=definitions
elif rtconfig.PLATFORM == 'mingw':
    libs = Split('''
        winmm
        gdi32
        winspool
        comdlg32
        advapi32
        shell32
        ole32
        oleaut32
        uuid
        odbc32
        odbccp32
        ''')
    TARGET = 'rtthread-win32.' + rtconfig.TARGET_EXT
    env = Environment(tools = ['mingw'],
        AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
        CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS,
        AR = rtconfig.AR, ARFLAGS = '-rc',
        LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
    env['LIBS']=libs
    env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
else:
    TARGET = 'rtthread'
    env.Append(CCFLAGS=rtconfig.CFLAGS)
    env.Append(LINKFLAGS=rtconfig.LFLAGS)
    env.Append(LIBS=['m'])

# prepare building environment
base_objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False, remove_components=['rtgui'])

if GetDepend('RT_USING_RTGUI'):
    sdl_lib = ['SDL', 'SDLmain']
    sdl_lib_path = [os.path.abspath(os.path.join(BSP_ROOT, 'SDL/lib/x86'))]
    sdl_inc_path = [os.path.abspath(os.path.join(BSP_ROOT, 'SDL/include'))]
    env.Append(LIBS=sdl_lib)
    env.Append(LIBPATH=sdl_lib_path)
    env.Append(CPPPATH=sdl_inc_path)
	

objs = base_objs

def ObjRemove(objs, remove):
    for item in objs:
        # print type(item), os.path.basename(str(item))
        if os.path.basename(str(item)) in remove:
             objs.remove(item)
    return

# build program  -shared
if GetDepend('RT_USING_MODULE'):
    # Remove module.c in $RTT_ROOT/src
    ObjRemove(objs, ['module.obj', 'module.o'])

    AddOption('--def',
      dest='def',
      action='store_true',
      default=False,
      help='create rthread.def of rtthread.dll on windows')
    if GetOption('def'):
        if rtconfig.PLATFORM != 'mingw':
            print "scons error: `--def' can only work with mingw"
            exit(1)

        env['LINKFLAGS'] = rtconfig.DEFFILE_LFLAGS
        env.SharedLibrary("build/rtthread.dll", objs)
        program = ''
    else:
        if rtconfig.PLATFORM == 'cl':
            objs += ['rtthread.def']
        elif rtconfig.PLATFORM == 'mingw':
            rtconfig.POST_ACTION = 'del /Q build/rtthread.lib \n rename build/librtthread.a build/rtthread.lib\n'
            # rtconfig.POST_ACTION = 'lib /machine:i386 /def:rtthread.def /out:rtthread.lib'
        env.SharedLibrary("build/rtthread.dll", objs)
        program = env.Program('build/' + TARGET, 'dummy.c', LIBS='rtthread', LIBPATH='build')
        EndBuilding('build/' + TARGET, program) # end building

else:
    program = env.Program('build/' + TARGET, objs)
    EndBuilding(TARGET, program)

	
print '\n------------------------' 
print '\n------------------------' 


